﻿using System;
using DocumentFormat.OpenXml;
using D = DocumentFormat.OpenXml.Drawing;
using P = DocumentFormat.OpenXml.Presentation;
using PK = DocumentFormat.OpenXml.Packaging;
using System.IO;
using Fantom;
using System.Linq;

namespace TestCore
{

	class Program
	{

		static void Main(string[] args)
		{
			var pathstr = Path.Combine(Environment.CurrentDirectory, @"OpenXml测试.pptx");
			TestLib(pathstr);
		}

		/// <summary>
		/// 测试核心库的解析功能。
		/// </summary>
		/// <param name="path"></param>
		static void TestLib(string pathstr)
		{
			var app = Application.Create();
			var pres = app.Presentations;
			pres.OnLoadingSlide += (_, e) =>
			{
				var s = string.Empty;

				switch (e.LoadingState)
				{
					case PresentationsLoadingOperationEnum.PackageLoadComplete:
						s = "包体导入已完成。";
						break;
					case PresentationsLoadingOperationEnum.SlidesReorderComplete:
						s = "序列初始化已完成。";
						break;
					case PresentationsLoadingOperationEnum.SlideIndexComplete:
						s = "幻灯片[{0}]已导入完成。";
						break;
					case PresentationsLoadingOperationEnum.SlideLoadComplete:
						s = "演示文稿导入完成。";
						break;
					default:
						break;
				}

				Console.WriteLine(s + "进度: {1}/{2}。", e.CurrentSlideID, e.Step, e.Steps);
			};

			var pre1 = pres.Load(pathstr);

			Console.WriteLine();
			Console.WriteLine("文件路径: {0}", pre1.Path);

			Console.WriteLine();
			Console.WriteLine("主题色：");
			foreach (var item in pre1.Theme.Colors)
			{
				Console.WriteLine("{0}, {1}",
					Enum.GetName(typeof(ThemeColorType), item.Key), item.Value);
			}

			Console.WriteLine();
			Console.WriteLine("幻灯片尺寸: {0}", pre1.SlideSize);

			Console.WriteLine("==============================================");
			Console.WriteLine("幻灯片信息：");
			foreach (ISlide slide in pre1.Slides)
			{
				Console.ForegroundColor = ConsoleColor.Yellow;
				Console.WriteLine("编号：{0}", slide.Index);
				Console.ForegroundColor = ConsoleColor.Gray;

				Console.WriteLine();
				Console.WriteLine("幻灯片自定义数据：");
				foreach (var xmlpart in slide.CustomXmlList)
				{
					Console.ForegroundColor = ConsoleColor.Green;
					Console.WriteLine(xmlpart.XDocument.Root.Name);
					Console.ForegroundColor = ConsoleColor.Gray;
				}

				Console.WriteLine();
				Console.WriteLine("图形信息：");
				foreach (IShape shape in slide.Shapes)
				{
					// 暂时不输出线条与组合的信息。
					if (shape.Type == ShapeType.Connection || shape.Type == ShapeType.Group)
					{
						Console.ForegroundColor = ConsoleColor.Red;
						Console.WriteLine("暂时不输出线条与组合的信息");
						Console.ForegroundColor = ConsoleColor.Gray;
						continue;
					}


					Console.ForegroundColor = ConsoleColor.Green;
					Console.WriteLine("名称：{0}", shape.Name);
					Console.ForegroundColor = ConsoleColor.Gray;
					Console.WriteLine("类型：{0}", shape.Type);

					var tran = shape.Transform;

					Console.WriteLine("预设: {0}", shape.PresetType);

					Console.WriteLine();
					Console.WriteLine("控点信息：");
					foreach (var adj in shape.Adjustments)
					{
						Console.Write("{0} ", adj);
					}
					Console.WriteLine();

					Console.WriteLine();
					Console.WriteLine("X：{0}", tran.AnchorTransform.Left.ToPixelValue());
					Console.WriteLine("Y：{0}", tran.AnchorTransform.Top.ToPixelValue());
					Console.WriteLine("宽度：{0}", tran.RectSize.Width.ToPixelValue());
					Console.WriteLine("高度：{0}", tran.RectSize.Height.ToPixelValue());
					Console.WriteLine("锚点水平比：{0}", tran.AnchorTransform.WidthRatio);
					Console.WriteLine("锚点垂直比：{0}", tran.AnchorTransform.HeightRatio);
					Console.WriteLine("旋转：{0}°", tran.Rotation.ToAngle());

					// 绘制文本。
					Console.WriteLine();
					Console.WriteLine("文本信息：");
					if (shape.TextBody != null)
						foreach (var para in shape.TextBody)
						{
							foreach (var run in para)
							{
								Console.WriteLine("字体：{0}", run.FontFamily);
								Console.WriteLine("大小：{0}", run.FontSize.ToFontSize());
								Console.WriteLine("内容：{0}", run.Text);
								if (run.Fill.Color != null)
									Console.WriteLine("填充：{0}", run.Fill.Color);

								Console.WriteLine();

							}
						}

					// 解析图片信息。
					Console.WriteLine();
					Console.WriteLine("填充信息：");
					if (shape.Fill.Color is Fantom.Drawing.BitmapColor)
					{
						Console.WriteLine("填充类型：图片");
						var media = (shape.Fill.Color as Fantom.Drawing.BitmapColor).Media;
						Console.WriteLine("路径：" + media.Uri);
						Console.WriteLine("大小：" + media.Bytes.Length);
					}
					else if (shape.Fill.Color is Fantom.Drawing.SolidColor)
					{
						Console.WriteLine("填充类型：纯色");
						var color = (shape.Fill.Color as Fantom.Drawing.SolidColor);
						Console.WriteLine("颜色：{0}", color);
					}
					else if (shape.Fill.Color is Fantom.Drawing.SchemeColor)
					{
						Console.WriteLine("填充类型：主题");
						var color = (shape.Fill.Color as Fantom.Drawing.SchemeColor);
						Console.WriteLine("主题色：{0}", color.ThemeColorType);
					}

					// 解析图片信息。
					Console.WriteLine();
					Console.WriteLine("线框信息：");
					Console.WriteLine("粗细：{0}", shape.Line.Thickness.ToPixelValue());
					if (shape.Line.Color is Fantom.Drawing.BitmapColor)
					{
						Console.WriteLine("填充类型：图片");
						var media = (shape.Line.Color as Fantom.Drawing.BitmapColor).Media;
						Console.WriteLine("路径：" + media.Uri);
						Console.WriteLine("大小：" + media.Bytes.Length);
					}
					else if (shape.Line.Color is Fantom.Drawing.SolidColor)
					{
						Console.WriteLine("填充类型：纯色");
						var color = (shape.Line.Color as Fantom.Drawing.SolidColor);
						Console.WriteLine("颜色：{0}", color);
					}
					else if (shape.Line.Color is Fantom.Drawing.SchemeColor)
					{
						Console.WriteLine("填充类型：主题");
						var color = (shape.Line.Color as Fantom.Drawing.SchemeColor);
						Console.WriteLine("主题色：{0}", color.ThemeColorType);
					}

					Console.WriteLine();
					Console.WriteLine("图形自定义信息：");
					foreach (var xmlpart in shape.CustomXmlList)
					{
						Console.WriteLine(xmlpart.XDocument.Root.Name);
					}

					Console.WriteLine();
					Console.WriteLine("图形标签信息：");
					foreach (var tag in shape.Tags)
					{
						Console.ForegroundColor = ConsoleColor.Yellow;
						Console.WriteLine("{0} {1}", tag.Key, tag.Value);
						Console.ForegroundColor = ConsoleColor.Gray;
					}

					Console.WriteLine();
					Console.WriteLine("动画信息：");
					WritelineSeq(slide.TimeLine.MainSequence);
					foreach (var seq in slide.TimeLine.InteractiveSequences)
					{
						WritelineSeq(seq);
					}
				}

				Console.WriteLine("==============================================");
			}
		}

		private static void WritelineSeq(IAnimationSequence seq)
		{
			
			

			foreach (var fra in seq)
			{
				Console.ForegroundColor = ConsoleColor.Black;
				Console.BackgroundColor = ConsoleColor.Green;
				Console.Write("片段：");
				Console.BackgroundColor = ConsoleColor.Black;
				Console.WriteLine();

				foreach (var bun in fra)
				{
					Console.BackgroundColor = ConsoleColor.Green;
					Console.Write("集束：");
					Console.BackgroundColor = ConsoleColor.Black;
					Console.WriteLine();
					foreach (var eff in bun)
					{
						Console.BackgroundColor = ConsoleColor.Yellow;
						Console.Write("效果：");
						Console.BackgroundColor = ConsoleColor.Black;
						Console.WriteLine();
						foreach (var bhv in eff)
						{
							Console.BackgroundColor = ConsoleColor.DarkYellow;
							Console.Write("行为：");
							Console.BackgroundColor = ConsoleColor.Black;
							Console.ForegroundColor = ConsoleColor.Gray;
							Console.WriteLine();
							Console.WriteLine(bhv);
						}
					}
				}
			}
		}

		/// <summary>
		/// 提取pptx文件中的重要信息。
		/// </summary>
		static void ExportDetail(string pathstr)
		{

			var predoc = PK.PresentationDocument.Open(pathstr, false);
			var slides = predoc.PresentationPart.SlideParts;

			foreach (var sld in slides)
			{
				var slide = sld.Slide;
				var timing = (P.Timing)null;
				// Console.WriteLine(sld.Slide.OuterXml);
				var alterContent = slide.GetFirstChild<AlternateContent>();

				if (alterContent != null)
				{
					var choice = alterContent.GetFirstChild<AlternateContentChoice>();
					if (choice != null)
					{
						timing = choice.GetFirstChild<P.Timing>();
					}

				}
				else
				{
					timing = slide.GetFirstChild<P.Timing>();
				}

				if (timing != null)
					Console.WriteLine(timing.OuterXml);
				Console.WriteLine();

			}
		}
	}

}
